package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.mapper.SkuMapper;
import com.leyou.item.service.SkuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {
    @Override
    public void minusStock(Map<Long, Integer> cartsMap) {
        this.executeBatch(sqlSession -> {
            //循环想sqlSession中添加statement，myBatis，map传参
            cartsMap.entrySet().forEach( cart ->{
                HashMap<String, Object> paramMap = new HashMap<>();
                paramMap.put("skuId",cart.getKey());
                paramMap.put("num",cart.getValue());

                sqlSession.update("com.leyou.item.mapper.SkuMapper.minusStock",paramMap);
            });
            //一次性刷入所有的statement达到批量执行的目的
            sqlSession.flushStatements();
        });
    }

    @Override
    public void saveSku(SpuDTO spuDTO) {
        List<SkuDTO> skuDTOS = spuDTO.getSkus();
        for (SkuDTO skuDTO : skuDTOS) {
            Sku sku = skuDTO.toEntity(Sku.class);
            sku.setSpuId(spuDTO.getId());
            this.save(sku);
        }
    }
}
